package com.cyt.work.wb.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.wb.dao.WxWbBulidDao;
import com.cyt.work.wb.model.po.WxWbBulid;
import com.cyt.work.wb.model.po.WxWbRoom;
import com.cyt.work.wb.model.vo.WxWbBulidVo;
import com.cyt.work.wb.model.vo.WxWbRoomVo;
import com.cyt.work.wb.service.WxWbBulidService;
import com.cyt.work.wb.service.WxWbRoomService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * 维保模块-建筑物表Service层实现类
 *
 * @author ding xu
 * @date 2019-11-25 8:51:23
 */
@Service("wxWbBulidService")
public class WxWbBulidServiceImpl extends BaseServiceImpl<WxWbBulid> implements WxWbBulidService {

    @Resource
    private WxWbBulidDao wxWbBulidDao;
    @Resource
    private WxWbRoomService wxWbRoomService;

    @Autowired
    public void setBaseDao(WxWbBulidDao wxWbBulidDao) {
        this.baseDao = wxWbBulidDao;
    }

    /**
     * 分页查询
     *
     * @param wxWbBulidVo 接受参数对象
     * @param pageNum 分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author ding xu
     * @date 2019-11-25 8:51:23
     */
    @Override
    public ReturnResult findPage(WxWbBulidVo wxWbBulidVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        Example example = new Example(WxWbBulid.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        List<WxWbBulid> list = wxWbBulidDao.selectByExample(example);
        PageInfo<WxWbBulid> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 分页查询
     *
     * @param wxWbBulidVo 接受参数对象
     * @param pageNum 分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author ding xu
     * @date 2019-11-25 8:51:23
     */
    @Override
    public ReturnResult findVoPage(WxWbBulidVo wxWbBulidVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WxWbBulidVo> list = wxWbBulidDao.selectVoList(wxWbBulidVo);
        PageInfo<WxWbBulidVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * list查询
     *
     * @param wxWbBulidVo 接受参数对象
     * @return List<WxWbBulid>
     * @author ding xu
     * @date 2019-11-25 8:51:23
     */
    @Override
    public List<WxWbBulid> findList(WxWbBulidVo wxWbBulidVo) {
        Example example = new Example(WxWbBulid.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(wxWbBulidVo.getName())){
            criteria.andLike("name", "%"+wxWbBulidVo.getName()+"%");
        }
        if(StringUtils.isNotBlank(wxWbBulidVo.getBulidType())){
            criteria.andEqualTo("bulidType", wxWbBulidVo.getBulidType());
        }
        if(StringUtils.isNotBlank(wxWbBulidVo.getBulidClass())){
            criteria.andEqualTo("bulidClass", wxWbBulidVo.getBulidClass());
        }
        if(StringUtils.isNotBlank(wxWbBulidVo.getApplyId())){
            criteria.andEqualTo("applyId", wxWbBulidVo.getApplyId());
        }
        if(StringUtils.isNotBlank(wxWbBulidVo.getParentIds())){
            criteria.andLike("parentIds", wxWbBulidVo.getParentIds());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        List<WxWbBulid> wxWbBulidList=wxWbBulidDao.selectByExample(example);
        return wxWbBulidList;
    }

    /**
     * list查询
     *
     * @param wxWbBulidVo 接受参数对象
     * @return List<WxWbBulid>
     * @author ding xu
     * @date 2019-11-25 8:51:23
     */
    @Override
    public List<WxWbBulid> findListTree(WxWbBulidVo wxWbBulidVo) {
        Example example = new Example(WxWbBulid.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        criteria.andLessThan("bulidClass","3");
        if(StringUtils.isNotBlank(wxWbBulidVo.getName())){
            criteria.andEqualTo("name", wxWbBulidVo.getName());
        }
        if(StringUtils.isNotBlank(wxWbBulidVo.getBulidType())){
            criteria.andEqualTo("bulidType", wxWbBulidVo.getBulidType());
        }
        if(StringUtils.isNotBlank(wxWbBulidVo.getApplyId())){
            criteria.andEqualTo("applyId", wxWbBulidVo.getApplyId());
        }
        return wxWbBulidDao.selectByExample(example);
    }

    /**
     * list查询
     *
     * @param wxWbBulidVo 接受参数对象
     * @return List<WxWbBulid>
     * @author ding xu
     * @date 2019-11-25 8:51:23
     */
    @Override
    public List<WxWbBulid> findListTreeRoom(WxWbBulidVo wxWbBulidVo) {
        Example example = new Example(WxWbBulid.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if(StringUtils.isNotBlank(wxWbBulidVo.getName())){
            criteria.andEqualTo("name", wxWbBulidVo.getName());
        }
        if(StringUtils.isNotBlank(wxWbBulidVo.getBulidType())){
            criteria.andEqualTo("bulidType", wxWbBulidVo.getBulidType());
        }
        if(StringUtils.isNotBlank(wxWbBulidVo.getBulidClass())){
            criteria.andEqualTo("bulidClass", wxWbBulidVo.getBulidClass());
        }
        if(StringUtils.isNotBlank(wxWbBulidVo.getApplyId())){
            criteria.andEqualTo("applyId", wxWbBulidVo.getApplyId());
        }
        return wxWbBulidDao.selectByExample(example);
    }

    /**
     * list查询
     *
     * @param wxWbBulidVo 接受参数对象
     * @return List<WxWbBulid>
     * @author ding xu
     * @date 2019-11-25 8:51:23
     */
    @Override
    public List<WxWbBulidVo> findVoList(WxWbBulidVo wxWbBulidVo) {
        return wxWbBulidDao.selectVoList(wxWbBulidVo);
    }

    /**
     * 单个对象查询
     *
     * @param wxWbBulidVo 接受参数对象
     * @return WxWbBulid
     * @author ding xu
     * @date 2019-11-25 8:51:23
     */
    @Override
    public WxWbBulid findOne(WxWbBulidVo wxWbBulidVo) {
        WxWbBulid wxWbBulid = new WxWbBulid();
        if (StringUtils.isBlank(wxWbBulidVo.getId())) {
            wxWbBulid.setId(wxWbBulidVo.getId());
        }
        return wxWbBulidDao.selectOne(wxWbBulid);
    }

    /**
     * 单个对象查询
     *
     * @param wxWbBulidVo 接受参数对象
     * @return WxWbBulid
     * @author ding xu
     * @date 2019-11-25 8:51:23
     */
    @Override
    public WxWbBulidVo findVoOne(WxWbBulidVo wxWbBulidVo) {
        return wxWbBulidDao.selectVoOne(wxWbBulidVo);
    }

    /**
     * 添加或更新
     *
     * @param wxWbBulidVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author ding xu
     * @date 2019-11-25 8:51:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WxWbBulidVo wxWbBulidVo, SysUser sysUser) {
        if(!wxWbBulidVo.getBulidClass().equals("3")){
            wxWbBulidVo.setBulidType(null);
        }
        if (StringUtils.isBlank(wxWbBulidVo.getId())) {
            WxWbBulid wxWbBulid = new WxWbBulid(sysUser);
            wxWbBulid.setApplyId(wxWbBulidVo.getApplyId());
            wxWbBulid.setName(wxWbBulidVo.getName());
            wxWbBulid.setBulidType(wxWbBulidVo.getBulidType());
            wxWbBulid.setBulidClass(wxWbBulidVo.getBulidClass());
            wxWbBulid.setRemarks(wxWbBulidVo.getRemarks());
            wxWbBulid.setParentId(wxWbBulidVo.getParentId());
            if(StringUtils.isBlank(wxWbBulidVo.getParentId())){
                wxWbBulid.setParentIds("0");
                wxWbBulid.setExcelModel(wxWbBulidVo.getName());
            }else{
                WxWbBulid wxWbBulidP=wxWbBulidDao.selectByPrimaryKey(wxWbBulidVo.getParentId());
                wxWbBulid.setParentIds(wxWbBulidP.getParentIds()+","+wxWbBulidVo.getParentId());
                wxWbBulid.setExcelModel(wxWbBulidP.getExcelModel()+"-"+wxWbBulidVo.getName());
            }
            wxWbBulid.setSort(Integer.parseInt(wxWbBulidVo.getSort()));
            wxWbBulidDao.insert(wxWbBulid);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            WxWbBulid wxWbBulid = wxWbBulidDao.selectByPrimaryKey(wxWbBulidVo.getId());
            String oldParentIds=wxWbBulid.getParentIds();
            String oldExcelModel=wxWbBulid.getExcelModel();
            wxWbBulid.setApplyId(wxWbBulidVo.getApplyId());
            wxWbBulid.setName(wxWbBulidVo.getName());
            wxWbBulid.setBulidType(wxWbBulidVo.getBulidType());
            wxWbBulid.setRemarks(wxWbBulidVo.getRemarks());
            wxWbBulid.setParentId(wxWbBulidVo.getParentId());
            wxWbBulid.setBulidClass(wxWbBulidVo.getBulidClass());
            if(StringUtils.isBlank(wxWbBulidVo.getParentId())){
                wxWbBulid.setParentIds("0");
                wxWbBulid.setExcelModel(wxWbBulid.getName());
            }else{
                WxWbBulid wxWbBulidP=wxWbBulidDao.selectByPrimaryKey(wxWbBulidVo.getParentId());
                wxWbBulid.setParentIds(wxWbBulidP.getParentIds()+","+wxWbBulidVo.getParentId());
                wxWbBulid.setExcelModel(wxWbBulidP.getExcelModel()+"-"+wxWbBulidVo.getName());
            }
            wxWbBulid.setSort(Integer.parseInt(wxWbBulidVo.getSort()));
            wxWbBulid.setUpdateTime(new Date());
            wxWbBulid.setUpdateUser(sysUser.getId());
            wxWbBulidDao.updateByPrimaryKey(wxWbBulid);


            //批处理子目录
            WxWbBulidVo wxWbBulidcs = new WxWbBulidVo();
            wxWbBulidcs.setParentIds("%"+wxWbBulid.getId()+"%");
            List<WxWbBulid> list = this.findList(wxWbBulidcs);
            for (WxWbBulid e : list){
                e.setParentIds(e.getParentIds().replace(oldParentIds, wxWbBulid.getParentIds()));
                e.setExcelModel(e.getExcelModel().replace(oldExcelModel,wxWbBulid.getExcelModel()));
                wxWbBulidDao.updateByPrimaryKey(e);
            }
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wxWbBulidVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author ding xu
     * @date 2019-11-25 8:51:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WxWbBulidVo wxWbBulidVo, SysUser sysUser) {
        //判断是否有子集
        int countC=wxWbBulidDao.countChild(wxWbBulidVo);
        if(countC>0){
            return new ReturnResult(ErrorCode.CODE_1, BaseConstant.BASE_DEL_RULE_ERROR);
        }
        //如果为建筑物，判断是否包含房间
        if(wxWbBulidVo.getBulidClass().equals("3")){
             WxWbRoomVo wxWbRoomVo=new WxWbRoomVo();
             wxWbRoomVo.setBulidId(wxWbBulidVo.getId());
             List<WxWbRoomVo>  list=wxWbRoomService.listExample(wxWbRoomVo);
             if(list.size()>0){
                 return new ReturnResult(ErrorCode.CODE_1, BaseConstant.BASE_DEL_RULE_ERROR);
             }
        }
        //修改字段
        WxWbBulid wxWbBulid = new WxWbBulid();
        wxWbBulid.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        wxWbBulid.setUpdateUser(sysUser.getId());
        wxWbBulid.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WxWbBulid.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wxWbBulidVo.getId())) {
            criteria.andIn("id", Arrays.asList(wxWbBulidVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        int i= wxWbBulidDao.updateByExampleSelective(wxWbBulid, example);
        if(i>0){
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_DEL);
        }else{
            return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_DEL);
        }

    }

    /**
     * 根据条件物理删除
     *
     * @param wxWbBulidVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author ding xu
     * @date 2019-11-25 8:51:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WxWbBulidVo wxWbBulidVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WxWbBulid.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wxWbBulidVo.getId())) {
            criteria.andIn("id", Arrays.asList(wxWbBulidVo.getId().split(",")));
        }
        wxWbBulidDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 获取建筑物树（bulidClass=3 为建筑物 不能作为父类进行选择，屏蔽）
     *
     * @param wxWbBulidVo 参数接受对象
     * @return ReturnResult
     * @author dingxu
     * @date 2019-8-15 17:15
     */
    @Override
    public ReturnResult dTreeList(WxWbBulidVo wxWbBulidVo) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<WxWbBulid> wxWbBulidList = this.findListTree(wxWbBulidVo);
        for (WxWbBulid wxWbBulid : wxWbBulidList) {
            Map<String, Object> resultMap = new LinkedHashMap<>();
            resultMap.put("id", wxWbBulid.getId());
            resultMap.put("title", wxWbBulid.getName());
            resultMap.put("parentId", StringUtils.isBlank(wxWbBulid.getParentId()) ? "0" : wxWbBulid.getParentId());
            Map<String, String> map = new LinkedHashMap<>();
            map.put("type", "0");
            map.put("isChecked", "0");
            List<Map<String, String>> checkArrList = new ArrayList<>();
            checkArrList.add(map);
            resultMap.put("checkArr", checkArrList);
            resultList.add(resultMap);
        }
        return new ReturnResult(resultList);
    }

    /**
     * 获取建筑物树（只有为建筑物才可以被选取）
     *
     * @param wxWbBulidVo 参数接受对象
     * @return ReturnResult
     * @author dingxu
     * @date 2019-8-15 17:15
     */
    @Override
    public ReturnResult dTreeListRoom(WxWbBulidVo wxWbBulidVo) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<WxWbBulid> wxWbBulidList = this.findListTreeRoom(wxWbBulidVo);
        for (WxWbBulid wxWbBulid : wxWbBulidList) {
            Map<String, Object> resultMap = new LinkedHashMap<>();
            resultMap.put("id", wxWbBulid.getId());
            resultMap.put("title", wxWbBulid.getName());
            resultMap.put("parentId", StringUtils.isBlank(wxWbBulid.getParentId()) ? "0" : wxWbBulid.getParentId());
            resultMap.put("isLast", wxWbBulid.getBulidClass().equals("3"));
            Map<String, String> map = new LinkedHashMap<>();
            map.put("type", "0");
            map.put("isChecked", "0");
            List<Map<String, String>> checkArrList = new ArrayList<>();
            checkArrList.add(map);
            resultMap.put("checkArr", checkArrList);
            resultList.add(resultMap);
        }
        return new ReturnResult(resultList);
    }

    /**
     * 获取建筑物树+房间（）
     *
     * @param wxWbBulidVo 参数接受对象
     * @return ReturnResult
     * @author dingxu
     * @date 2019-8-15 17:15
     */
    @Override
    public ReturnResult dTreeListBulidAndRoom(WxWbBulidVo wxWbBulidVo) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<WxWbBulid> wxWbBulidList = this.findList(wxWbBulidVo);
        WxWbRoomVo wxWbRoomVo=new WxWbRoomVo();
        List<WxWbRoomVo> wxWbRoomList =wxWbRoomService.listExample(wxWbRoomVo);
        for(WxWbRoomVo w:wxWbRoomList){
            WxWbBulid wxWbBulid =new WxWbBulid();
            wxWbBulid.setId(w.getId());
            wxWbBulid.setName(w.getRoomName());
            wxWbBulid.setParentId(w.getBulidId());
            wxWbBulid.setBulidClass("4");
            wxWbBulidList.add(wxWbBulid);
        }
        for (WxWbBulid wxWbBulid : wxWbBulidList) {
            Map<String, Object> resultMap = new LinkedHashMap<>();
            resultMap.put("id", wxWbBulid.getId());
            resultMap.put("title", wxWbBulid.getName());
            resultMap.put("parentId", StringUtils.isBlank(wxWbBulid.getParentId()) ? "0" : wxWbBulid.getParentId());
            resultMap.put("isLast",wxWbBulid.getBulidClass().equals("4"));
            Map<String, String> map = new LinkedHashMap<>();
            map.put("type", "0");
            map.put("isChecked", "0");
            List<Map<String, String>> checkArrList = new ArrayList<>();
            checkArrList.add(map);
            resultMap.put("checkArr", checkArrList);
            resultList.add(resultMap);
        }
        return new ReturnResult(resultList);
    }
}